﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using VietImage.Roles;
using VietImage.Models;

namespace VietImage.Controllers
{
    public class AccountController : Controller
    {
        //
        // GET: /Account/
        public void GetLanguage()
        {
            HttpCookie languageCookie = null;
            if (languageCookie == null)
            {
                languageCookie = new HttpCookie("Language");
                languageCookie.Value = "2";
            }
            languageCookie.Expires = DateTime.Now.AddMonths(1);
            //Response.SetCookie(languageCookie);
            ViewData["Language"] = int.Parse(languageCookie.Value);
        }

        public ActionResult Index()
        {
            GetLanguage();
            RegisteredUserRole role = (RegisteredUserRole)Session["Role"];
            if (role != null)
            {
                ViewData["CountryList"] = Country.GetList();
                ViewData["CityList"] = City.GetList(role.UserInfo.City.ID_Country);
            }
            return View();
        }

        public ActionResult Translation()
        {
            return View("Translation");
        }

        public ActionResult InformationList()
        {
            return View("InformationList");
        }

        public ActionResult HistoryTranslation()
        {
            return View("HistoryTranslation");
        }

        public ActionResult ReportView()
        {
            return View("ReportView");
        }

        public ActionResult Logout()
        {
            RegisteredUserRole role = (RegisteredUserRole)Session["Role"];
            if (role != null)
            {
                role.Logout();
                Session.Remove("Role");
            }
            return PartialView("UserMenu");
        }

        public ActionResult Login(String loginusername = "", String loginpassword = "", String logincurrenturl = "/")
        {
            RegisteredUserRole role = (RegisteredUserRole)Session["Role"];
            if (role == null && loginusername.Trim() != "" && loginpassword.Trim() != "")
            {
                role = new RegisteredUserRole();
                if (role.Login(loginusername, loginpassword))
                {
                    RegisteredUserRole newRole = new RegisteredUserRole();
                    switch (role.UserInfo.ID_Permission)
                    {
                        case 1:
                            newRole = new OwnerRole();
                            break;
                        case 2:
                            //newRole = new AdministratorRole();
                            break;
                        case 3:
                            newRole = new TranslatorRole();
                            break;
                        case 4:
                            newRole = new PartnerRole();
                            break;
                        case 5:
                            newRole = new CustomerRole();
                            break;
                    }
                    newRole.ID_Language = role.ID_Language;
                    newRole.UserInfo = role.UserInfo;
                    Session["Role"] = newRole;
                    role = newRole;
                    if (role.ID_Language != 0)
                    {
                        HttpCookie languageCookie = Request.Cookies["Language"];
                        if (languageCookie == null)
                        {
                            languageCookie = new HttpCookie("Language");
                        }
                        languageCookie.Value = role.ID_Language.ToString();
                        languageCookie.Expires = DateTime.Now.AddMonths(1);
                        Response.SetCookie(languageCookie);
                    }
                }
            }
            return Redirect(logincurrenturl);
        }
    }
}
